package com.oim.core.business.service;

import com.oim.core.app.AppContext;
import com.oim.core.bean.UserData;
import com.oim.core.bean.UserHead;
import com.oim.core.business.box.PersonalBox;
import com.oim.core.business.manage.SystemManage;
import com.oim.core.business.sender.UserSender;
import com.oim.core.common.AppConstant;
import com.oim.core.common.app.service.Service;
import com.oim.core.common.app.view.LoginView;
import com.oim.core.common.app.view.MainView;
import com.oim.core.common.app.view.TrayView;
import com.oim.core.common.config.ConfigManage;
import com.oim.core.common.data.UserSaveData;
import com.oim.core.common.data.UserSaveDataBox;
import com.oim.core.common.task.ExecuteTask;
import com.oim.core.common.util.FileLockUtil;
import com.oim.core.net.message.data.LoginData;
import com.only.common.result.ErrorInfo;
import com.only.common.result.Info;

/**
 * 描述：
 *
 * @author XiaHui
 * @date 2016年1月7日 下午8:37:18
 * @version 0.0.1
 */
public class PersonalService extends Service {

	public PersonalService(AppContext appContext) {
		super(appContext);
	}

	public void login() {

	}

	/**
	 * 登录后回掉
	 *
	 * @param info
	 * @param user
	 */
	public void loginBack(final Info info, final UserData user, UserHead userHead) {
		boolean isLogin = info.isSuccess();
		LoginView loginView = this.appContext.getSingleView(LoginView.class);
		if (isLogin) {

			PersonalBox pb = appContext.getBox(PersonalBox.class);
			pb.setUserHead(userHead);
			pb.setUserData(user);

			loginView.setVisible(false);
			MainView mainView = this.appContext.getSingleView(MainView.class);
			mainView.setVisible(true);
			mainView.setUser(user);
			TrayView trayView = this.appContext.getSingleView(TrayView.class);
			trayView.showAllMenu(true);

			ExecuteTask et = new ExecuteTask() {

				@Override
				public void execute() {
					
					SystemManage sm = appContext.getManage(SystemManage.class);
					sm.initApp(user);
					PersonalBox pb=appContext.getBox(PersonalBox.class);
					
					LoginData loginData = pb.getLoginData();
					UserSender us = appContext.getSender(UserSender.class);
					
					us.sendUpdateStatus(loginData.getStatus());
					saveUserData(user, userHead);
				}
			};
			appContext.add(et);

		} else {
			this.appContext.getConnectThread().setAutoCloseConnectTime(1);
			if (null != info.getWarnings() && !info.getWarnings().isEmpty()) {
				ErrorInfo e = info.getWarnings().get(0);
				loginView.showPrompt(e.getText());
			} else if (null != info.getErrors() && !info.getErrors().isEmpty()) {
				ErrorInfo e = info.getErrors().get(0);
				loginView.showPrompt(e.getText());
			} else {
				loginView.showPrompt("登录失败");
			}
			PersonalBox pb=appContext.getBox(PersonalBox.class);
			LoginData loginData = pb.getLoginData();
			String accountLock = AppConstant.userHome + "/" + AppConstant.app_home_path + "/lock/"
					+ loginData.getAccount() + ".Lock";
			FileLockUtil.releaseLock(accountLock);
		}
	}

	private void saveUserData(UserData userData, UserHead userHead) {
		UserSaveDataBox usdb = (UserSaveDataBox) ConfigManage.get(UserSaveDataBox.path, UserSaveDataBox.class);
		int size = usdb.getSize();// 获取登录保存的用户账号数量
		if (size > 20) {// 默认只保存20个用户的数量，多于20个，则删除多余的。
			usdb.remove((size - 20));
		}
		if (null == userHead) {
			userHead = new UserHead();
			userHead.setHeadId(userData.getHead());
			userHead.setType(UserHead.type_system);
		}
		UserSaveData usd = new UserSaveData();
		usd.setAccount(userData.getAccount());
		usd.setUserHead(userHead);
		usdb.put(userData.getAccount(), usd);
		ConfigManage.addOrUpdate(UserSaveDataBox.path, usdb);
	}

	

	public void handleDifferentLogin() {
		this.appContext.getConnectThread().setAutoConnect(false);// 另外登录了账号后，设置为不自动连接
		this.appContext.getConnectThread().closeConnect();// 关闭连接
		MainView mainView = this.appContext.getSingleView(MainView.class);
		mainView.setVisible(true);
		mainView.shwoDifferentLogin();// 弹出被另外登录提示框
	}
}
